
import axios from 'axios'
//导入NProgress对应的js和css
import NProgress from 'nprogress'

axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'
axios.interceptors.request.use(config=>{

   //request拦截器 添加进度条
   NProgress.start();
      //判断是否携带token
    config.headers.Authorization=window.sessionStorage.getItem('token')
    return config
})

axios.interceptors.response.use(config=>{
   
   //response 隐藏进度条
     NProgress.done()
     return config
 })
const request = class {
    constructor(url1, arg1) {
        this.url = url1
        this.arg = arg1

    }

    modePost() {

        return new Promise((resolve, reject) => {
            axios.post(this.url, this.arg).then(res => {
                resolve(res.data);
            }).catch(err => {
                reject("出错了")
            })
        })
    }

    modeGet() {
        console.log("请求的参数",this.url,this.arg)
        return new Promise((resolve, reject) => {
            axios.get(this.url,{params:this.arg}).then(res => {
                resolve(res.data);
            }).catch(err => {
                reject("出错了",err)
            })
        })
    }

    modePut(){
        return new Promise((resolve, reject) => {
            axios.put(this.url,this.arg).then(res => {
                resolve(res.data);
            }).catch(err => {
                reject("出错了")
            })
        })
    }

    modeDel(){
        return new Promise((resolve, reject) => {
            axios.delete(this.url,this.arg).then(res => {
                resolve(res.data);
            }).catch(err => {
                reject("出错了")
            })
        })
    }
}
export default request